package burubaka.controller.top;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;

import net.arnx.jsonic.JSON;

import org.slim3.controller.Navigation;
import org.slim3.util.BeanUtil;

import burubaka.BurubakaConstants;
import burubaka.common.controller.Controller;
import burubaka.model.BbBbs;
import burubaka.service.BbBbsService;

public class BbBbsController extends Controller {

	private static final Logger log = Logger.getLogger(BbBbsController.class.getName());

	private BbBbsService bbBbsService = new BbBbsService();

	/**
	 * 投稿
	 * 
	 * @return Navigation
	 * @throws Exception
	 */
	public Navigation post() throws Exception {

		BbBbs bbBbs = new BbBbs();
		Map<String, String> map = JSON.decode((String) reqMap.get("bbBbs"), HashMap.class);
		BeanUtil.copy(map, bbBbs);

		bbBbsService.regist(bbBbs);

		List<BbBbs> bbBbsList = bbBbsService.findAll(BurubakaConstants.BBS_MAX_LINE);
		Map<String, Object> resMap = new HashMap<String, Object>();
		resMap.put("bbBbsLine", bbBbsList);
		String json = JSON.encode(resMap, true);
		log.fine(json);
		setJson(json);

		return null;
	}
}
